package com.example.springbootwl08.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springbootwl08.common.componet.PageResult;
import com.example.springbootwl08.common.componet.R;
import com.example.springbootwl08.product.pojo.dto.OrderPageDTO;
import com.example.springbootwl08.product.pojo.entity.Order;
import com.example.springbootwl08.product.pojo.vo.OrderVO;
import com.example.springbootwl08.product.service.OrderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


/**
 * @author 宗雨
 * @create 2024-02-27 14:44
 * project_name:ssm
 * @Description
 */
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping
    public IPage<Order> getOrderList(OrderPageDTO orderPageDTO) {
        //第一种方式 mybatis 分页
        //PageResult pageResult = orderService.selectPage(orderPageDTO);

        //mybatisplus QueryWrapper 分页
        Page<Order> page = new Page<>(orderPageDTO.getPagenum(), orderPageDTO.getPagesize());
        QueryWrapper<Order> wrapper = new QueryWrapper<>();

        //根据前端传递的参数动态构建查询条件
        if (orderPageDTO.getUserId() != null) {
            wrapper.eq("user_id", orderPageDTO.getUserId());
        }
        if (orderPageDTO.getPayStatus() != null) {
            wrapper.eq("pay_status", orderPageDTO.getPayStatus());
        }
        if (orderPageDTO.getIsSend() != null) {
            wrapper.eq("is_send", orderPageDTO.getIsSend());
        }
        if (orderPageDTO.getOrderFapiaoTitle() != null) {
            wrapper.eq("order_fapiao_title", orderPageDTO.getOrderFapiaoTitle());
        }
        if (orderPageDTO.getOrderFapiaoCompany() != null) {
            wrapper.eq("order_fapiao_company", orderPageDTO.getOrderFapiaoCompany());
        }
        if (orderPageDTO.getOrderFapiaoContent() != null) {
            wrapper.eq("order_fapiao_content", orderPageDTO.getOrderFapiaoContent());
        }
        if (orderPageDTO.getConsigneeAddr() != null) {
            wrapper.eq("consignee_addr", orderPageDTO.getConsigneeAddr());
        }


        IPage<Order> iPage =orderService.page(page,wrapper);

        return iPage;
    }

    /**
     * 根据id查询订单信息
     * @param
     * @return
     */
    @GetMapping("/{id}")
    public R<Order> getOrderById(@PathVariable("id") Integer order_id) {
        Order order = orderService.getOrderById(order_id);
        return R.ok(order);
    }
    @PutMapping
    public R<OrderVO>  updateOrder(@RequestBody Order order) {
        OrderVO orderVO =  orderService.updateOrder(order);
        return  R.ok(orderVO);
    }

}
